<?php

declare(strict_types=1);
/**
 * This file is part of MoChat.
 * @link     https://mo.chat
 * @document https://mochat.wiki
 * @contact  group@mo.chat
 * @license  https://github.com/mochat-cloud/mochat/blob/master/LICENSE
 */
namespace MoChat\App\WorkMessage\Task;

use Hyperf\Di\Annotation\Inject;
use Hyperf\Logger\LoggerFactory;
use MoChat\App\WorkMessage\Fetcher\MessageDataFetcher;
use MoChat\App\WorkMessage\Queue\MessagePullQueue;

/**
 * 会话存档消息拉取.
 */
class MessageSyncDataTask
{
    /**
     * @var \Psr\Log\LoggerInterface
     */
    protected $logger;

    /**
     * @Inject
     * @var MessageDataFetcher
     */
    private $messageDataFetcher;

    /**
     * @Inject
     * @var MessagePullQueue
     */
    private $messagePullQueue;

    public function __construct(LoggerFactory $loggerFactory)
    {
        $this->logger = $loggerFactory->get(__CLASS__);
    }

    public function execute()
    {
        $corpIds = $this->messageDataFetcher->getDataSource();

        if (empty($corpIds)) {
            $this->logger->debug('没有满足条件同步消息的企业');
            return;
        }

        foreach ($corpIds as $corpId) {
            $this->logger->debug(sprintf('加入拉取会话消息队列，企业ID：%s', (string) $corpId));
            $this->messagePullQueue->handle((int) $corpId);
        }
    }
}
